import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  // site: 'PCOnline' // 当前站点
  site: localStorage.getItem('site') || 'PCAuto', // 当前站点
  biz: localStorage.getItem('biz') || 'CMS',
  bizList: ['CMS', 'PGC', 'HJ', ''],
  contentTypeObj: {
    Article: '文章',
    Video: '视频',
    AD: '广告',
    Product: '产品',
    Question: '问答',
    Post: '帖子',
    Audio: '音频',
    ShortMessage: '短消息',
    Course: '课程',
    Other: '其他'
  },
  auditStatus: { Pending: '待审', Auditing: '审核中', Publish: '通过', Deleted: '删除' }
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  TOGGLE_SITE: (state, site) => {
    state.site = site
    localStorage.setItem('site', site)
  },
  TOGGLE_BIZ: (state, biz) => {
    state.biz = biz
    localStorage.setItem('biz', biz)
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  toggleSite({ commit }, status) {
    console.log('dispatch', status)
    commit('TOGGLE_SITE', status)
  },
  toggleBiz({ commit }, biz) {
    console.log('dispatch biz', biz)
    commit('TOGGLE_BIZ', biz)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
